<template>
  <div>
    <table>
      <thead>
        <tr>
          <th>ID</th>
          <th>用户名</th>
          <th>用户名</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="user in users" :key="user.id">
          <td>{{ user.id }}</td>
          <td>{{ user.account }}</td>
          <td>{{ user.realName }}</td>
        </tr>
      </tbody>
    </table>
    <div class="pagination">
      <button @click="prevPage" :disabled="pageNo === 1">Previous</button>
      <span>Page {{ pageNo }} of {{ totalPage }}</span>
      <button @click="nextPage" :disabled="pageNo === totalPage">Next</button>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      users: [],
      totalCount: 0,
      totalPage: 0,
      pageNo: 1,
      pageSize: 5,
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      try {
        const response = await axios.get('/user/list', {
          params: {
            pageNo: this.pageNo,
            pageSize: this.pageSize,
          },
        });
        const { list, totalCount } = response.data;
        this.users = list;
        this.totalCount = totalCount;
        this.totalPage = Math.ceil(totalCount / this.pageSize);
      } catch (error) {
        console.error('Error fetching data:', error);
      }
    },
    prevPage() {
      if (this.pageNo > 1) {
        this.pageNo--;
        this.fetchData();
      }
    },
    nextPage() {
      if (this.pageNo < this.totalPage) {
        this.pageNo++;
        this.fetchData();
      }
    },
  },
};
</script>

<style scoped>
table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  border: 1px solid #ddd;
  padding: 8px;
}

th {
  background-color: #f2f2f2;
}

.pagination {
  margin-top: 20px;
  text-align: center;
}

button {
  margin: 0 10px;
  padding: 5px 10px;
}
</style>
